//
//  GameScreen.cpp
//  match2
//
//  Created by Tri Vo on 7/13/13.
//
//

#include "GameScreen.h"
#include "MenuScreen.h"
#include "SoundManager.h"

using namespace cocos2d;
using namespace std;

#pragma mark - Scene Init, Constructor & Destructor

CCScene* GameScreen::scene() {
    CCScene *scene = CCScene::create();
    GameScreen *layer = GameScreen::create();
    scene->addChild(layer);
    return scene;
}

bool GameScreen::init() {
    if (!CCLayer::init()) {
        return false;
    }
    
    generateContent();

    return true;
}

GameScreen::~GameScreen() {
    if (pmArray) {
        pmArray->release();
    }
    if (pmArraySelectedNumber) {
        pmArraySelectedNumber->release();
    }
}

#pragma mark - Methods

void GameScreen::generateWinScreen() {
    clearContents();
    CCSize winSize = GameController::getWinSize();
    
    // Local variables
    int fontSizeForTitle = GameController::getFontSizeForTitle();
    char *fontNameForTitle = GameController::getFontNameForTitle();
    
    // Congratulation Message
    CCLabelTTF *pLblCongrats = CCLabelTTF::create("Congratulations", fontNameForTitle, fontSizeForTitle);
    pLblCongrats->setPosition(ccp(200, winSize.height/2 + 50));
    addChild(pLblCongrats);
    
    CCLabelTTF *pLblYouWin = CCLabelTTF::create("You won", fontNameForTitle, fontSizeForTitle);
    pLblYouWin->setPosition(ccp(200, winSize.height/2));
    addChild(pLblYouWin);
    
    ///// HUD
    // Menu new game
    CCMenuItemFont *pMenuNewGame = CCMenuItemFont::create("New game", this, menu_selector(GameScreen::newGame));
    // Menu back to main menu
    CCMenuItemFont *pMenuMainMenu = CCMenuItemFont::create("Main menu", this, menu_selector(GameScreen::backToMainScreen));
    // Menu HUD
    CCMenu *pMenuHUD = CCMenu::create(pMenuNewGame, pMenuMainMenu, NULL);
    pMenuHUD->setPosition(ccp(winSize.width - 150, winSize.height/2 - 50));
    pMenuHUD->alignItemsVerticallyWithPadding(30);
    pMenuHUD->setScale(0.7);
    addChild(pMenuHUD);
}

void GameScreen::generateContent() {
    // Clear all content in this scene
    clearContents();
    // Generate Matrix
    createMatrix();
    mBeginX = 30;
    mBeginY = 0;
    CCSize winSize = GameController::getWinSize();
    ///// HUD
    // Menu new game
    CCMenuItemFont *pMenuNewGame = CCMenuItemFont::create("New game", this, menu_selector(GameScreen::newGame));
    // Menu back to main menu
    CCMenuItemFont *pMenuMainMenu = CCMenuItemFont::create("Main menu", this, menu_selector(GameScreen::backToMainScreen));
    // Menu HUD
    CCMenu *pMenuHUD = CCMenu::create(pMenuNewGame, pMenuMainMenu, NULL);
    pMenuHUD->setPosition(ccp(winSize.width - 150, winSize.height/2 - 100));
    pMenuHUD->alignItemsVerticallyWithPadding(30);
    pMenuHUD->setScale(0.7);
    addChild(pMenuHUD);
    pmMoveCount = 0;
    CCLabelTTF *lblMoveCount = CCLabelTTF::create("Moves count", "Arial", 24);
    lblMoveCount->setPosition(ccp(winSize.width - 80, winSize.height/2 + 80));
    addChild(lblMoveCount);
    
    pmLabelMoveCount = CCLabelTTF::create("0", "Arial", 24);
    pmLabelMoveCount->setPosition(ccp(winSize.width - 100, winSize.height/2 + 50));
    char count[1] = {0};
    sprintf(count, "0");
    pmLabelMoveCount->setString(count);
    addChild(pmLabelMoveCount);
    
    //////// MAIN CONTENT
    if (pmArraySelectedNumber == NULL) {
        pmArraySelectedNumber = new CCArray();
    }
    if (pmArray == NULL) {
        pmArray = new CCArray();
    }
    
    int countRow = 1;
    char strFileName[100] = {0};
    CCMenu *currentMenu = NULL;
    for (int i = 0; i < 4; i++) {
        countRow = i;
        switch (countRow) {
            case 0:
            {
                if (pmMenuRow_1 == NULL) {
                    pmMenuRow_1 = CCMenu::create();
                }
                currentMenu = pmMenuRow_1;
            }
                break;
            case 1:
            {
                if (pmMenuRow_2 == NULL) {
                    pmMenuRow_2 = CCMenu::create();
                }
                currentMenu = pmMenuRow_2;
            }
                break;
            case 2:
            {
                if (pmMenuRow_3 == NULL) {
                    pmMenuRow_3 = CCMenu::create();
                }
                currentMenu = pmMenuRow_3;
            }
                break;
            case 3:
            {
                if (pmMenuRow_4 == NULL) {
                    pmMenuRow_4 = CCMenu::create();
                }
                currentMenu = pmMenuRow_4;
            }
                break;
            default:
                break;
        }
        for (int j = 0; j < 5; j++) {
            int randNumber = mMatrix[i][j];
            sprintf(strFileName, "summericons_100px_%02d.png", randNumber);
            CCMenuItemSprite *pTile = CCMenuItemSprite::create(CCSprite::create("question_image.jpg"), CCSprite::create(strFileName), this, menu_selector(GameScreen::imageClicked));
            pTile->setScale(0.5f);
            pTile->setTag(randNumber);
            currentMenu->addChild(pTile);
            pmArray->addObject(pTile);
        }
        currentMenu->setPosition(ccp(170, mBeginY + 70));
        currentMenu->alignItemsHorizontallyWithPadding(10);
        addChild(currentMenu);
        mBeginY += 60;
    }
}



#pragma mark - Actions

void GameScreen::imageClicked(CCMenuItemSprite *pSender) {
    CCMenuItemSprite *pTile = pSender;
    pmMoveCount++;
    this->updateMoveCount();
    char fileName[100] = {0};
    sprintf(fileName, "summericons_100px_%02d.png", pTile->getTag());
    if (pmArraySelectedNumber->containsObject(pTile)) {
        pTile->stopAllActions();
        pmArraySelectedNumber->removeObject(pTile);
        pTile->setScale(0.5f);
        pmPreviousSelectedItem = NULL;
        SoundManager::getInstance()->playSound(SOUND_BUTTON_CLICK, false);
        pTile->setNormalImage(CCSprite::create("question_image.jpg"));
    } else {
        if (pmArraySelectedNumber->count() < 2) {
            if (pmPreviousSelectedItem == NULL) {
                pmPreviousSelectedItem = pTile;
                CCActionInterval *pActionScaleOut = CCScaleTo::create(0.2f, 0.7f);
                CCActionInterval *pActionScaleIn = CCScaleTo::create(0.2f, 0.5f);
                CCRepeatForever *pActionRepeatForever = CCRepeatForever::create(CCSequence::create(CCArray::create(pActionScaleOut, pActionScaleIn, NULL)));
                pTile->runAction(pActionRepeatForever);
                pmArraySelectedNumber->addObject(pTile);
                SoundManager::getInstance()->playSound(SOUND_BUTTON_CLICK, false);
                pTile->setNormalImage(CCSprite::create(fileName));
                
            } else {
                if (pmPreviousSelectedItem->getTag() == pTile->getTag() && pmPreviousSelectedItem != pTile) {
                    pTile->setNormalImage(CCSprite::create(fileName));
                    pmPreviousSelectedItem->removeFromParentAndCleanup(true);
                    pTile->removeFromParentAndCleanup(true);
                    pmArray->removeObject(pTile);
                    pmArray->removeObject(pmPreviousSelectedItem);
                    // Match-up success
                    SoundManager::getInstance()->playSound(SOUND_MATCH_SUCCESS, false);
                    } else {
                    pmPreviousSelectedItem->stopAllActions();
                    pTile->stopAllActions();
                    pmPreviousSelectedItem->setScale(0.5f);
                    pTile->setScale(0.5f);
                    // Match-up fail
                    SoundManager::getInstance()->playSound(SOUND_BUTTON_CLICK, false);
                        pmPreviousSelectedItem->setNormalImage(CCSprite::create("question_image.jpg"));
                }
                pmArraySelectedNumber->removeAllObjects();
                pmPreviousSelectedItem = NULL;
                if (pmArray->count() == 0) {
                    generateWinScreen();
                }
            }
        } else {
            for (int i = 0; i < pmArraySelectedNumber->count(); i++) {
                CCMenuItemSprite *pTempSprite = reinterpret_cast<CCMenuItemSprite *>(pmArraySelectedNumber->objectAtIndex(i));
                pTempSprite->stopAllActions();
                pTempSprite->setScale(0.5f);
            }
            pmPreviousSelectedItem->setNormalImage(CCSprite::create("question_image.jpg"));
            pTile->setNormalImage(CCSprite::create("question_image.jpg"));
            pmArraySelectedNumber->removeAllObjects();
            CCActionInterval *pActionScaleOut = CCScaleTo::create(0.2f, 0.7f);
            CCActionInterval *pActionScaleIn = CCScaleTo::create(0.2f, 0.5f);
            CCRepeatForever *pActionRepeatForever = CCRepeatForever::create(CCSequence::create(CCArray::create(pActionScaleOut, pActionScaleIn, NULL)));
            pTile->runAction(pActionRepeatForever);
            pmArraySelectedNumber->addObject(pTile);
            SoundManager::getInstance()->playSound(SOUND_BUTTON_CLICK, false);
        }
    }
}

void GameScreen::newGame(CCObject *pSender) {
    SoundManager::getInstance()->playSound(SOUND_MENU_CLICK, false);
    this->removeAllChildrenWithCleanup(true);
    generateContent();
}

void GameScreen::backToMainScreen(CCObject *pSender) {
    SoundManager::getInstance()->playSound(SOUND_MENU_CLICK, false);
    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.5f, MenuScreen::scene()));
}